/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package explorer;

/**
 *
 * @author Laetitia
 */

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public class FileTreeModel implements TreeModel {

    private FileModel root;
    private ArrayList<String> _extensionsFilter = new ArrayList<String>();
    
    private boolean _showHidden = false;
    private boolean _showFolder = true;
    private boolean _showFile = false;

    public FileTreeModel(FileModel file) {
        root = file;
    }

    public FileTreeModel(File root) {
        this(new FileModel(root));
    }

    public List getFiles(Object parent) {
        
        FileModel parentFile = (FileModel) parent;
        File[] myFiles = new File(parentFile.getPath()).listFiles();
        
        if (myFiles == null) {
            return null;

        }
        
        FileModel aModel;
        ArrayList<FileModel> arrayOfMyFiles = new ArrayList<FileModel>();
//        ArrayList<File> arrayOfMyFiles = new ArrayList(Arrays.asList(myFiles));

        boolean show;
        for (File f : myFiles) {
            
            aModel = new FileModel(f);
            
            if (!_extensionsFilter.isEmpty() && aModel.isFile()) {
                show = false;
                for (String ext : _extensionsFilter) {
                    if (aModel.getPath().endsWith(ext)) {
                        show = true;
                    }
                }
                
                if (!show) {
                    continue;
                }
            }
            
            if (aModel.isDirectory() && _showFolder) {
                arrayOfMyFiles.add(aModel);
            } else if (aModel.isFile() && _showFile) {
                arrayOfMyFiles.add(aModel);
            } else if (aModel.isHidden() && _showHidden) {
                arrayOfMyFiles.add(aModel);
            }
        }
        
        Collections.sort(arrayOfMyFiles, new Comparator<FileModel>() {

            @Override
            public int compare(FileModel f1, FileModel f2) {
                boolean dirf1 = f1.isDirectory();
                boolean dirf2 = f2.isDirectory();
                if (dirf1 && !dirf2) {
                    return -1;
                }
                if (!dirf1 && dirf2) {
                    return 1;
                }
                return f1.getPath().compareTo(f2.getPath());
            }
        });
        
        
        
        return arrayOfMyFiles;
    }
    
    public void showFile(boolean value) {
        _showFile = value;
    }
    
    public void showFolder(boolean value) {
        _showFolder = value;
    }
    
    public void showHidden(boolean value) {
        _showHidden = value;
    }
    
    public void addExtension(String ext) {
        _extensionsFilter.add(ext);
    }

    @Override
    public Object getRoot() {
        return root;
    }

    @Override
    public Object getChild(Object parent, int index) {
        return getFiles((FileModel) parent).get(index);
    }

    @Override
    public int getChildCount(Object parent) {        
        List fl = getFiles((FileModel) parent);
        if (fl == null)
            return 0;
        return fl.size();
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        return getFiles((FileModel) parent).indexOf(child);
    }

    @Override
    public boolean isLeaf(Object node) {
        FileModel f = (FileModel) node;
        return !f.isDirectory();
    }

    @Override
    public void valueForPathChanged(TreePath path, Object newValue) {
    }

    @Override
    public void removeTreeModelListener(TreeModelListener l) {
    }

    @Override
    public void addTreeModelListener(TreeModelListener l) {
        
    }
}
